package com.wsc.wisdomchinaqinghe.activitity.wisdomParty;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.fragment.PartyFragment;
import com.wsc.wisdomchinaqinghe.view.MyToast;
import com.wsc.wisdomchinaqinghe.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;


public class PartyActivity extends BaseFragmentActivity implements View.OnClickListener{
    private ImageView iv_back;
    private MyToast myToast;
    private Context context;
    private String cid;
    private String pId;
    private ImageView iv_user;
    private ViewPager viewPager;
    private PagerSlidingTabStrip tabs;// 新闻栏滑动条
    private List<String> listData;
    private List<Fragment> lastlistfragmentView;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.party_layout);
        Intent intent=getIntent();
        cid=intent.getStringExtra("cId");
        pId=intent.getStringExtra("pId");
        myApplication = (MyApplication)getApplication();
        initView();
        listData=new ArrayList<>();
        listData.add("党员学习");
        listData.add("在线答题");
        listData.add("党建活动");
        listData.add("通讯录");
        listData.add("公告通知");
        if(listData!=null && listData.size()>0 )
        {
            showTabsViewPager();
        }
    }

    private void  showTabsViewPager()
    {
        lastlistfragmentView = new ArrayList<>();
        List<String> lastEightModuleFragment = new ArrayList<>();
        List<String> nameList=new ArrayList<>();
        for(String data:listData)
        {
            Bundle bundle = new Bundle();
            bundle.putString("cid",cid);
            bundle.putString("pId",pId);
            bundle.putString("name",data);
            PartyFragment receptionCenterFragment = PartyFragment.newInstance(bundle);
            lastlistfragmentView.add(receptionCenterFragment);
            lastEightModuleFragment.add(PartyFragment.class.getSimpleName() + data);
            nameList.add(data);
        }
        myApplication.setInformationFragmentSelected(lastEightModuleFragment.get(0));
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), lastlistfragmentView, nameList));// 设置viewpageradapter
        tabs.setViewPager(viewPager, lastlistfragmentView, lastEightModuleFragment, this);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> listfragmentView;// viewList 集合
        private List<String> TITLES;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView, List<String> TITLES) {
            super(fm);
            this.listfragmentView = listfragmentView;
            this.TITLES = TITLES;
            // TODO Auto-generated constructor stub
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return TITLES.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listfragmentView.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return TITLES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            Fragment ff = (Fragment) super.instantiateItem(container, position);
            ff.setTargetFragment(listfragmentView.get(position), position);

            return ff;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }

    }


    private void initView() {
        context = this;
        myToast = new MyToast(context);
        iv_back=(ImageView) findViewById(R.id.iv_back);
        iv_user=(ImageView) findViewById(R.id.iv_user);
        iv_back.setOnClickListener(this);
        iv_user.setOnClickListener(this);

        viewPager = (ViewPager)findViewById(R.id.viewPager);
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setIndicatorHeight(Tools.dip2px(context, 3));
        tabs.setTextSize(Tools.dip2px(context, 17));
        tabs.setTextColor(getResources().getColor(R.color.d7white));
        tabs.setSelectedTextColor(getResources().getColor(R.color.white));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_user:
                Intent intent=new Intent(context,PartyUserActivity.class);
                startActivityForResult(intent,0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            finish();
        }
    }

}
